<?php

/*
 * This file is part of the symfony package.
 * (c) Walter Seewald <walterseewald@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfWidgetFormInvoice represents a invoice widget.
 *
 * @package    symfony
 * @subpackage widget
 * @author     Walter Seewald <walterseewald@gmail.com>
 * @version    sfWidgetFormCUIT.class.php 1.0 2010-08-25 16:40:19 $
 */
class sfWidgetFormCUIT extends sfWidgetFormInput
{
  /**
   * Configures the current widget.
   *
   * Available options:
   *
   *  * tipo:         An string representing the type of the entity (2 chars)
   *  * numero:       An string representing the cuit number (8 chars)
   *  * control:      An string representing the control number of the CUIT (1 chars)
   *  * can_be_empty: Whether the widget accept an empty value (false by default)
   *  * empty_values: An array of values to use for the empty value (empty string for talonario and factura by default)
   *
   * @param array $options     An array of options
   * @param array $attributes  An array of default HTML attributes
   *
   * @see sfWidgetForm
   */
  protected function configure($options = array(), $attributes = array())
  {
    $this->addOption('can_be_empty', false);
    $this->addOption('empty_values', array('tipo' => '', 'numero' => '','control' => ''));
  }

  /**
   * @param  string $name        The element name
   * @param  string $value       The date displayed in this widget EJ 20-30813043-7  => 2038130437
   * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
   * @param  array  $errors      An array of errors for the field
   *
   * @return string An HTML tag string
   *
   * @see sfWidgetForm
   */
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    // convert value to an array

	if(!empty($value)){
		if(!is_array($value)){
			// Si no es un array, combina 2 array, para que quede los bloques de digitos asociados a posiciones
			
			$value1['tipo'] = substr($value,0,2);
			$value1['numero'] = substr($value,2,8);
			$value1['control'] = substr($value,10,1);
			$value = $value1;
			//$value = array_combine(array('tipo','numero','control'),explode('-',$value));
			
		}
	}else{
		$value = $this->getOption('empty_values');
		if(empty($value))	$value = array("tipo" => "00","numero" => "00000000","control" => "0");
	}

    $cuit = array();
	$cuitN = $value;
    $cuit['%tipo%'] = $this->renderTipoWidget($name.'[tipo]', $cuitN['tipo'], array('type' => 'text'), array_merge($this->attributes, array('size' => 2,'maxlength' => 2)));
    $cuit['%numero%'] = $this->renderNumeroWidget($name.'[numero]', $cuitN['numero'], array('type' => 'text'), array_merge($this->attributes, array('size' => 8,'maxlength' => 8)));
    $cuit['%control%'] = $this->renderControlWidget($name.'[control]', $cuitN['control'], array('type' => 'text'), array_merge($this->attributes, array('size' => 1, 'maxlength' => 1)));

    return implode('-',$cuit);
  }

  /**
   * @param string $name
   * @param string $value
   * @param array $options
   * @param array $attributes
   * @return string rendered widget
   */
  protected function renderTipoWidget($name, $value, $options, $attributes)
  {
    $widget = new sfWidgetFormInputText($options, $attributes);
    return $widget->render($name, $value);
  }

  /**
   * @param string $name
   * @param string $value
   * @param array $options
   * @param array $attributes
   * @return string rendered widget
   */
  protected function renderNumeroWidget($name, $value, $options, $attributes)
  {
    $widget = new sfWidgetFormInputText($options, $attributes);
    return $widget->render($name, $value);
  }
  
  /**
   * @param string $name
   * @param string $value
   * @param array $options
   * @param array $attributes
   * @return string rendered widget
   */
  protected function renderControlWidget($name, $value, $options, $attributes)
  {
    $widget = new sfWidgetFormInputText($options, $attributes);
    return $widget->render($name, $value);
  }
}
